package firing;

import game.Bullet;
import game.Enemy;
import game.Player;

import java.util.List;

/** Node that chooses left or right node depending on current energy */
public class EnergyBranch extends AbstractBinaryNode {

    private static final long serialVersionUID = -5854563691255856345L;
    /** determines whether to choose left or right node */
    private int energyThreshold;
    
    public EnergyBranch() {
	super();
	randomizeNode();
    }
    
    /** Chooses left branch if energy is less than threshold, right otherwise */
    public void calculate(List<Bullet> bullets, double angle, Enemy shooter, Player player) {
	if (shooter.getEnergy() < energyThreshold && left != null) {
	    left.calculate(bullets, angle, shooter, player);
	} else if (right != null) {
	    right.calculate(bullets, angle, shooter, player);
	}
    }

    @Override
    public void randomizeNode() {
	energyThreshold = (Enemy.MAX_ENERGY * (int)(Math.random()*5)/5);
    }
    
    public int getEnergyThreshold() {
	return energyThreshold;
    }
    
    public void setEnergyThreshold(int threshold) {
	energyThreshold = threshold;
    }
    
    public String toString() {
	return ("Energy Branch: " + energyThreshold);
    }

    @Override
    public Node duplicate() {
	EnergyBranch duplicate = new EnergyBranch();
	duplicate.energyThreshold = energyThreshold;
	if (left != null) {
	    duplicate.setLeftChild(left.duplicate());
	}
	if (right != null) {
	    duplicate.setRightChild(right.duplicate());
	}
	return duplicate;
    }
}
